﻿using System;
using System.Text;
using DBUtil;
using DotNetCommon.Extensions;
using NUnit.Framework;

namespace Test.MySql
{
    [TestFixture]
    public class TestBase
    {
        protected DBAccess db = null;
        public TestBase()
        {
            DBUtil.DBFactory.AddDBSupport<DBUtil.Provider.MySql.MySqlDBFactory>();
            //mysql 8.0.12
            //db = DBFactory.CreateDB("MySql", "Server=192.168.0.19;Database=test;Uid=root;Pwd=123456;Charset=utf8;Max Pool Size=20000;");
            //mysql 5.7
            //db = DBFactory.CreateDB("MySql", "Server=192.168.0.18;Database=test;Uid=root;Pwd=123456;Charset=utf8;Max Pool Size=20000;");
            //为了支持 BulkCopy 数据库还需要设置: show variables like '%local_infile%'
            db = DBFactory.CreateDB("MySql", "Server=127.0.0.1;Database=test;Uid=root;Pwd=123456;AllowLoadLocalInfile=true;");
            //db = DBFactory.CreateDB("MySql", "Server=192.168.0.563;Database=test;Uid=root;Pwd=123456;Charset=utf8;Max Pool Size=20000;");
        }

        public void PrepareColumnTypes()
        {
            db.Manage.DropTableIfExist("test");
            db.ExecuteSql(@"CREATE TABLE test (
	t_bigint BIGINT NULL,
	t_bigint_unsigned BIGINT UNSIGNED NULL,
	t_binary BINARY(50) NULL,
	t_bit BIT(32) NULL,
	t_blob BLOB NULL,
	t_bool BOOL NULL,
	t_char CHAR(50) NULL,
	t_date DATE NULL,
	t_datetime DATETIME NULL,
	t_decimal DECIMAL NULL,
	t_double DOUBLE NULL,
	t_double_precision DOUBLE PRECISION NULL,
	t_enum ENUM('reg','green','blue') NULL,
	t_float FLOAT NULL,
	t_int INT NULL,
	t_int_unsigned INT UNSIGNED NULL,
	t_integer INTEGER NULL,
	t_integer_unsigned INTEGER UNSIGNED NULL,
	t_long_varbinary LONG VARBINARY NULL,
	t_long_varchar LONG VARCHAR NULL,
	t_longblob LONGBLOB NULL,
	t_longtext LONGTEXT NULL,
	t_mediumblob MEDIUMBLOB NULL,
	t_mediumint MEDIUMINT NULL,
	t_mediumint_unsigned MEDIUMINT UNSIGNED NULL,
	t_mediumtext MEDIUMTEXT NULL,
	t_numeric NUMERIC NULL,
	t_real REAL NULL,
	t_set SET('red','green','blue') NULL,
	t_smallint SMALLINT NULL,
	t_smallint_unsigned SMALLINT UNSIGNED NULL,
	t_text TEXT NULL,
	t_time TIME NULL,
	t_timestamp TIMESTAMP NULL,
	t_tinyblob TINYBLOB NULL,
	t_tinyint TINYINT NULL,
	t_tinyint_unsigned TINYINT UNSIGNED NULL,
	t_tinytext TINYTEXT NULL,
	t_varbinary VARBINARY(100) NULL,
	t_varchar VARCHAR(100) NULL,
	t_year YEAR NULL,
	t_json json NULL
)");
            var res = db.Insert("test",
                new
                {
                    t_bigint = 1,
                    t_bigint_unsigned = 2,
                    t_binary = Encoding.UTF8.GetBytes("t_binary"),
                    t_bit = 0,
                    t_blob = Encoding.UTF8.GetBytes("t_blob"),
                    t_bool = true,
                    t_char = 'c',
                    t_date = DateTime.Now.ToCommonDateString(),
                    t_datetime = DateTime.Now,
                    t_decimal = 12.354515,
                    t_double = 12.3645,
                    t_double_precision = 14.023,
                    t_enum = "blue",
                    t_float = 0.56326,
                    t_int = 3,
                    t_int_unsigned = 4,
                    t_integer = 5,
                    t_integer_unsigned = 6,
                    t_long_varbinary = Encoding.UTF8.GetBytes("t_long_varbinary"),
                    t_long_varchar = "t_long_varchar",
                    t_longblob = Encoding.UTF8.GetBytes("t_longblob"),
                    t_longtext = "t_longtext",
                    t_mediumblob = Encoding.UTF8.GetBytes("t_mediumblob"),
                    t_mediumint = 7,
                    t_mediumint_unsigned = 8,
                    t_mediumtext = "t_mediumtext",
                    t_numeric = 9.69,
                    t_real = 10.23,
                    t_set = "red,blue",
                    t_smallint = 11,
                    t_smallint_unsigned = 12,
                    t_text = "t_text",
                    t_time = DateTime.Now.ToCommonTimeString(),
                    t_tinyblob = Encoding.UTF8.GetBytes("t_tinyblob"),
                    t_tinyint = 13,
                    t_tinyint_unsigned = 14,
                    t_tinytext = "t_tinytext",
                    t_varbinary = Encoding.UTF8.GetBytes("t_varbinary"),
                    t_varchar = "t_varchar",
                    t_year = DateTime.Now.Year,
                    t_json = new { name = "小明", age = 20 }.ToJson()
                }.ToDictionary());
            Assert.IsTrue(res == 1);
        }
    }
}
